package com.ftar.urgent.http;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.ftar.urgent.http.model.CommonJson;
import com.ftar.urgent.http.model.CommonJsonList;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


/**
 * @author: wei.jin
 * @date: 2023/7/8
 * @email: wei.jin@fanttec.com
 * @description：
 */
public class FtHttpResponseHandler<T> implements Callback {
    private String TAG = this.getClass().getSimpleName();
    private Context mContext;
    private Class mCls;
    private String mDownloadPath;
    private int dbId;


    private Handler mHandler = new Handler(Looper.getMainLooper());

    public FtHttpResponseHandler(Context context, Class cls) {
        this(context, cls, false);
    }

    public FtHttpResponseHandler(Context context, Class cls,
                                 boolean showDialog) {
        this.mContext = context;
        this.mCls = cls;
        this.mDownloadPath = "";
        this.dbId = 0;
    }

    public FtHttpResponseHandler(Context context, String path) {
        this.mContext = context;
        this.mDownloadPath = path;
    }

    public FtHttpResponseHandler(Context context, Class cls,
                                 boolean showDialog, boolean netUnStableShowToast) {
        this.mContext = context;
        this.mCls = cls;
        this.mDownloadPath = "";
        this.dbId = 0;
    }

    private Boolean handleSpecialErrorCode(int code) {
        Boolean ret = false;

        return ret;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                onFailure(e.getMessage());
            }
        });
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
//        Log.d(TAG, "onResponse:" + response.body().string() + " result:" + response.isSuccessful());
        if(mDownloadPath == null || mDownloadPath.isEmpty()) {
            if (response.isSuccessful()) {
                String responseString = response.body().string();
                try {
                    final CommonJson<T> commonJson = GsonUtils.fromJson(responseString, mCls);
                    if (commonJson.getCode() == 0) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                onSuccess(commonJson);
                            }
                        });
                    } else {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                onFailure("Error, code:" + commonJson.getCode());
                            }
                        });
                    }
                } catch (Exception e) {
                    Log.e(TAG, "onSuccess Exception:" + e.toString());
                    try {
                        final CommonJsonList<T> commonJsonList = GsonUtils.fromJsonList(
                                responseString, mCls);
                        if (commonJsonList.getCode() == 0) {
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    onSuccess(commonJsonList);
                                }
                            });
                        } else {
                            mHandler.post(new Runnable() {
                                @Override
                                public void run() {
                                    onFailure("Error, code:" + commonJsonList.getCode());
                                }
                            });
                        }
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                onFailure("Parse error:" + e.toString());
                            }
                        });
                    }
                }
            } else {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        onFailure("error:" + response.message());
                    }
                });
            }
        }else if (!mDownloadPath.isEmpty()) {
        }
    }

    public void onDownLoadSuccess() {
    }

    public void onDownLoading(int progress) {
    }

    public void onSuccess(CommonJson<T> json) {
    }

    public void onSuccess(CommonJsonList<T> json) {
    }

    public Boolean onFailure(String errMessage) {
        return false;
    }

    public Context getContext() {
        return mContext;
    }


}
